package rs.mbs2.epics.server.session;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.Comment;
import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.ProfileImage;
import rs.mbs2.epics.server.entity.Rating;
import rs.mbs2.epics.server.entity.User;

@Stateless
@Remote(Init.class)
public class InitBean implements Init {
	
	public final String tomeeFolder = "C:\\apache-tomee-plus-1.5.0\\webapps\\ProjectMBS2Epics\\ProjectMBS2Epics\\Files\\";

	@PersistenceContext(unitName = "ProjectMBS2Epics")
	EntityManager em;
	
	public void init() {
		
		User user1 = new User("admin@admin.com", "admin", "Admin", "Adminovic", "MALE", new Date(), true, null);
		User user2 = new User("branko@branko.com", "branko", "Branko", "Milosavljevic", "MALE", new Date(), true, null);
		User user3 = new User("goca@goca.com", "goca", "Gordana", "Milosavljevic", "FEMALE", new Date(), false, null);
		em.persist(user1);
		em.persist(user2);
		em.persist(user3);
		
		
		ProfileImage defaultProfileImage1 = new ProfileImage("Files/default.jpg", "Files/default_thumbnail.jpg", 400, 400, (long) 12, new Date(), user1);
		defaultProfileImage1.setUser(user1);
		em.persist(defaultProfileImage1);
		
		ProfileImage defaultProfileImage2 = new ProfileImage("Files/default.jpg", "Files/default_thumbnail.jpg", 400, 400, (long) 12, new Date(), user2);
		defaultProfileImage2.setUser(user2);
		em.persist(defaultProfileImage2);
		
		ProfileImage defaultProfileImage3 = new ProfileImage("Files/default.jpg", "Files/default_thumbnail.jpg", 400, 400, (long) 12, new Date(), user3);
		defaultProfileImage3.setUser(user3);
		em.persist(defaultProfileImage3);
		

		
		Album album12 = new Album("Album1 usera 2", "Opis album1", true, user2);
		Album album22 = new Album("Album2 usera 2", "Opis album2", true, user2);
		Album album13 = new Album("Album1 usera 3", "Opis album1", true, user3);
		
		em.persist(album12);
		em.persist(album22);
		em.persist(album13);
		

		String photo1Loc = "Files/" + user2.getId() + "/" + album12.getId() +"/";
		String photo1name = "dog.jpg";
		Photo photo1 = new Photo(photo1Loc + photo1name , 400, 400, (long) 20, new Date(), "dog", "slika1 opis 400 xxx", true, false, false, false, 0.0, photo1Loc + "thumbnails/" + photo1name);
		copyPhotoAndThumbnail(user2.getId(), album12.getId(), photo1name);
		
		
		String descriptionOver200 = "ovo je slika na kojoj nisu stvarno fudbaleri neymar ronaldinho i messi, nego su to zemske koje lice na njih, to je fora u stvari. Ne znam sta bih jos ukucao da ovaj opis ima vise od 200 karaktera. Valjda je sad ovaj opis presao 200 karaktera.";
		String photo2Loc = "Files/" + user2.getId() + "/" + album12.getId() +"/";
		String photo2name = "nrm.jpg";
		Photo photo2 = new Photo(photo2Loc+photo2name , 800, 800, (long) 20, new Date(), "nrm", descriptionOver200, true, true, true, true, 0.0, photo2Loc + "thumbnails/"+photo2name);
		copyPhotoAndThumbnail(user2.getId(), album12.getId(), photo2name);

		
		String photo3Loc = "Files/" + user2.getId() + "/" + album22.getId() +"/";
		String photo3name =  "dogs.jpg";
		Photo photo3 = new Photo(photo3Loc+photo3name, 800, 800, (long) 20, new Date(), "dogs", "slika3 opis 800 xxx not visible", true, true, true, true, 0.0, photo3Loc + "thumbnails/"+photo3name);
		copyPhotoAndThumbnail(user2.getId(), album22.getId(), photo3name);

		
		String photo4Loc = "Files/" + user3.getId() + "/" + album13.getId() +"/";
		String photo4name = "exit.jpg";
		Photo photo4 = new Photo(photo4Loc+photo4name , 800, 800, (long) 20, new Date(), "exit", "slika4 opis 800 xxx not visible", true, true, true, true, 0.0, photo4Loc + "thumbnails/"+photo4name);
		copyPhotoAndThumbnail(user3.getId(), album13.getId(), photo4name);

		
		
		em.persist(photo1);
		em.persist(photo2);
		em.persist(photo3);
		em.persist(photo4);
		
		
		album12.getPhotos().add(photo1);
		album12.getPhotos().add(photo2);
		album22.getPhotos().add(photo3);
		album13.getPhotos().add(photo4);
		em.merge(album12);
		em.merge(album22);
		em.merge(album13);
		

		
		Comment comment1 = new Comment("dog title #1", "dog comment", new Date(), user1, photo1);
		em.persist(comment1);
		Comment comment2 = new Comment("dog title #2", "comment #2 on dog", new Date(), user2, photo1);
		em.persist(comment2);
		Comment comment3 = new Comment("nrm", "neymar ronaldo mess", new Date(), user1, photo2);
		em.persist(comment3);
		
		/*
		photo1.getComments().add(comment1);
		em.merge(photo1);
		photo1.getComments().add(comment2);
		em.merge(photo1);
		photo2.getComments().add(comment3);
		em.merge(photo2);
		user1.getComments().add(comment1);
		user1.getComments().add(comment3);
		user2.getComments().add(comment2);
		em.merge(user1);
		em.merge(user2);

		
		
		
		System.out.println("*********************************");
		System.out.println("size photo1 comments: " + photo1.getComments().size());
		for(Comment c : photo1.getComments()) {
			System.out.println("Current comment id: " + c.getId());
		}
		
		System.out.println("size photo2 comments: " + photo2.getComments().size());
		System.out.println("size user1 comments: " + user1.getComments().size());
		System.out.println("size user2 comments: " + user2.getComments().size());

		*/
		
		
		Rating rating1 = new Rating(new Integer(5), user1, photo1);
		Rating rating2 = new Rating(new Integer(4), user3, photo1);
		Rating rating3 = new Rating(new Integer(3), user2, photo2);
		Rating rating4 = new Rating(new Integer(2), user3, photo3);
		em.persist(rating1);
		em.persist(rating2);
		em.persist(rating3);
		em.persist(rating4);
		
		

	}
	
	private void copyPhotoAndThumbnail(int userId, int albumId, String photoName) { 
		
		// Photo
		String userFolderPath = tomeeFolder + userId;
		File userFolderTest = new File( userFolderPath);
		
		if(!userFolderTest.exists()) 	// ako ne postoji folder, treba da se napravi
			userFolderTest.mkdir();
				// napravljeno
		
		String folderForImagePath = userFolderPath + "\\" + albumId;
		File folderForImageTest = new File(folderForImagePath);
		
		if(!folderForImageTest.exists()) 	// ako ne postoji folder, treba da se napravi
			folderForImageTest.mkdir();
		
		String destPath = folderForImagePath + "\\" + photoName;
		
		
		File dest = new File(destPath);		
		File source = new File(tomeeFolder + "slikeInitBean\\" + photoName);
		try {
			Files.copy(source.toPath(), dest.toPath());
		} catch (IOException e) {
			System.out.println("IO Exception kopiranje slika u tomee");
			e.printStackTrace();
		}
		
		
		
		// Thumbnail
		
		String folderForImagePathT = folderForImagePath + "\\thumbnails";
		File folderForImageTestT = new File(folderForImagePathT);
		
		if(!folderForImageTestT.exists()) 	// ako ne postoji folder, treba da se napravi
			folderForImageTestT.mkdir();
		
		
		
		String destPathT = folderForImagePathT + "\\" + photoName;
		
		
		File destT = new File(destPathT);		
		File sourceT = new File(tomeeFolder + "slikeInitBean\\thumbnails\\" + photoName);
		try {
			Files.copy(sourceT.toPath(), destT.toPath());
		} catch (IOException e) {
			System.out.println("IO Exception kopiranje thumbnail slika u tomee");
			e.printStackTrace();
		}
		
	}
}
